<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/2/19
 * Time: 18:03
 */
namespace app\api\controller;
use think\Db;
class My extends Base {
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        header('Access-Control-Allow-Origin:*');
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }
    /**
     * 获取个人信息
     * @return \think\response\Json
     */
    public function userInfo(){
        extract(input());
        $user = $this->getUserInfo($user_id);
        if($user['user_sign']){
            $sign_yes = Db::table('up_sign')->where('sign_id','in',$user['user_sign'])->select();
            $sign_no = Db::table('up_sign')->where('sign_id','not in',$user['user_sign'])->select();
        }else{
            $sign_yes = [];
            $sign_no = Db::table('up_sign')->select();;
        }
        //1、轮播
        $lunbo = Db::table('up_banner')
            ->where('banner_type',4)
            ->where('banner_status',1)
            ->order('banner_sort desc')
            ->field('banner_id,banner_img,banner_title')
            ->select();
        return json(['status'=>200,'msg'=>$user,'lunbo'=>$lunbo,'sign_yes'=>$sign_yes,'sign_no'=>$sign_no]);

    }
    /**
     * 修改个人信息
     * @return \think\response\Json
     */
    public function userInfoEdit(){
        extract(input());
        $today = date('Y-m-d H:i:s');
        $data = [];
        $data['user_updatetime'] = $today;
        if(isset($user_image) && $user_image){
            $data['user_image'] = $user_image;
        }
        if(isset($user_username) && $user_username){
            $data['user_username'] = $user_username;
        }
        if(isset($user_sign) && $user_sign){
            $data['user_sign'] = $user_sign;
        }
        $where['user_id'] = $user_id;
        $res = Db::table('up_u_user')->where($where)->update($data);
        if($res){
            return json(array('status'=>200,'msg'=>"修改成功"));
        }else{
            return json(array('status'=>0,'msg'=>"修改失败"));
        }

    }

    /**
     * 我的浏览记录
     *
     * @return \think\response\Json
     */
    public function myNewsHistory(){
        extract(input());
        $where = [
            'his_user_id' => $user_id
        ];
        $field = 'new_id,new_title,new_thumb,new_sign_left,new_sign_right,new_video,new_comment,new_zan';
        $field .= ',his_id,his_time';
        $data = Db::table('up_news_history his')
            ->join('up_news n','his.his_new_id = n.new_id','left')
            ->where($where)
            ->order('his_time desc')
            ->page($page,20)
            ->field($field)
            ->select();
        return json(array('status'=>200,'msg'=>$data));
    }
    /**
     * 我关注的专家
     * @return \think\response\Json
     */
    public function myLikes(){
        extract(input());
        $where = [
            'like_user_id' => $user_id
        ];
        $data = Db::table('up_like l')
            ->join('up_u_user u','l.like_other_user=u.user_id','LEFT')
            ->where($where)
            ->order('like_time desc')
            ->page($page,20)
            ->field('like_id,like_time,like_other_user,u.*')
            ->select();
        if($data){
            foreach ($data as $k => $v){
                $nums = Db::table('up_like')
                    ->where('like_other_user',$v['like_other_user'])
                    ->count();
                $data[$k]['nums'] = $nums;
                //
                if($v['user_tags']){
                    $data[$k]['user_tags'] = explode(',',$v['user_tags']);
                }else{
                    $data[$k]['user_tags'] = [];
                }
            }
        }
        return json(array('status'=>200,'msg'=>$data));
    }
    /**
     * 我关注的专家——取消关注
     * @return \think\response\Json
     */
    public function myLikesEdit(){
        extract(input());
        $where = [
            'like_id' => $like_id
        ];
        $res = Db::table('up_like')
            ->where($where)
            ->delete();
        if($res){
            return json(array('status'=>200,'msg'=>'ok'));
        }else{
            return json(array('status'=>10,'msg'=>'error'));
        }
    }
    /**
     * 常见问题
     * @return \think\response\Json
     */
    public function questions(){
        extract(input());
        $list = Db::table('up_question')
            ->where('q_status',1)
            ->field('q_content',true)
            ->page($page,20)
            ->order('q_time desc')
            ->select();
        return json(['status'=>200,'msg'=>$list]);

    }
    /**
     * 常见问题详情
     * @return \think\response\Json
     */
    public function questionInfo(){
        extract(input());
        $where['q_id'] = $q_id;
        $data = Db::table('up_question')
            ->where($where)
            ->find();
        return json(['status'=>200,'msg'=>$data]);

    }
    public function feedback(){
            extract(input());
        $data = [
            'f_user_id' => $user_id,
            'feedback_content' => $content,
            'feedback_phone' => $phone,
            'feedback_time' => date('Y-m-d H:i:s'),
        ];
        $res = Db::table('up_feedback')->insert($data);
        if($res){
            return json(array('status'=>200,'msg'=>"成功"));
        }else{
            return json(array('status'=>0,'msg'=>"失败"));
        }
    }
}